﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace NASM_Develop_IDE
{
    public partial class BuildDialog : Form
    {
        
        private Solution solution;
        private StringBuilder output;
        public BuildDialog(Solution solution)
        {
            output = new StringBuilder();
            if (solution == null)
            {
                throw new ArgumentNullException("Argument 'solution' Is Not Pointing To A Valid Object!");
            }
            this.solution = solution;
            InitializeComponent();
            TextBoxHighlighter.Highlight(extraParameters, "Enter Extra Command Line Parameters (Both -f and -o; Including Incorrect Parameters Will Be Ignored)");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public string CompilationOutput
        {
            get
            {
                if (output.ToString() == string.Empty)
                {
                    return "No Compilation Output! Please Check If A Problem Occured In The 'bin' Folder!";
                }
                else
                {
                    return output.ToString();
                }
            }
        }
        public bool LinkAfterCompilation
        {
            get
            {
                return linkWithGCC.Checked;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {

                switch(0)
                {
                    case 0: //WIN32
                    {
                        
                        List<string> sources = solution.AbsolutePathOfSources;
                        int nthObject = 1;
                        sources.ForEach(delegate(string source)
                        {
                            StringBuilder arguments = new StringBuilder("-fwin32");
                            arguments.Append(" ");
                            arguments.Append("\"");
                            arguments.Append(source);
                            arguments.Append("\"");
                            arguments.Append(" ");
                            if (formatSourceName.Checked)
                            {
                                arguments.Append(" -o\"");
                                arguments.Append(solution.AbsolutePathOfBinFolder + "\\" + Path.GetFileNameWithoutExtension(source));
                                arguments.Append(".obj\"");
              
                            }
                            else
                            {
                                arguments.Append(" -o\"");
                                arguments.Append(solution.AbsolutePathOfBinFolder + "\\" + nthObject);
                                arguments.Append(".obj\"");
                            
                            }
                            if (generateListingFile.Checked)
                                arguments.Append(" -l\"" + solution.AbsolutePathOfBinFolder + "\\" + Path.GetFileNameWithoutExtension(source) + ".lst\"");
                            if (extraParameters.Text != "")
                                arguments.Append(" " + extraParameters.Text);
                            StringBuilder buildOutput;
                            if (NASMBuilder.Compile(arguments.ToString(), out buildOutput))
                            {
                                if (buildOutput != null)
                                {
                                    this.output.Append(buildOutput + "\n");
                                }
                            }
                            nthObject++;
                        });
                        break;
                    }
                }
                GC.Collect();
            
        }

        private void extraParameters_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.F || e.KeyCode == Keys.L || e.KeyCode == Keys.O) && !Control.IsKeyLocked(Keys.CapsLock))
            {
               
                e.SuppressKeyPress = true; 
            }
        }
    }
}
